<template>
  <div class="app-container">
      <!--搜索区-->
      <el-card class="filter-container" shadow="never">
          <div>
              <i class="el-icon-search"></i>
              <span>筛选搜索</span>
              <el-button style="float:right" type="primary" @click="handleQuery()" size="small">
                  查询搜索
              </el-button>
              <el-button style="float:right;margin-right: 15px" @click="resetQuery()" size="small">
                  重置
              </el-button>
              <div style="margin-top: 15px">
                  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  </el-form>
              </div>
          </div>
      </el-card>

      <!--操作按钮区-->
      <el-card class="operate-container" shadow="never">
          <i class="el-icon-tickets"></i>
          <span>数据列表</span>
          <el-button style="margin-left: 20px" type="primary" plain icon="el-icon-circle-plus" size="mini" @click="handleAdd" v-hasPermi="['website:slide:create']">新增</el-button>
          <el-button type="success" plain icon="el-icon-edit-outline" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['website:slide:edit']">修改</el-button>
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['website:slide:remove']">删除</el-button>
      </el-card>

      <!--数据列表区-->
      <div class="table-container">
          <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange" border>
            <el-table-column label="排序" width="55" prop="sortOrder" align="center" />
            <el-table-column label="ID" width="55" prop="slideItemId" align="center" />
            <el-table-column label="幻灯片标题"  prop="title" align="center" />
            <el-table-column label="描述"  prop="description" align="center" />
            <el-table-column label="链接"  prop="url" align="center" />
            <el-table-column label="打开方式"  prop="target" align="center" />
            <el-table-column label="图片"  align="center" >
              <template slot-scope="scope">
                <el-button style="font-size: 25px" size="mini" type="text" icon="el-icon-picture" @click="handlePreview(scope.row.image)"></el-button>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="isShow">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_show_list" :value="scope.row.isShow"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['website:slide:edit']"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['website:slide:remove']"
                    >删除</el-button>
                </template>
            </el-table-column>
          </el-table>
          <el-dialog :visible.sync="dialogVisible" width="600px">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
      </div>

      <!--分页区-->
      <div class="pagination-container">
          <el-pagination
                  layout="total, sizes,prev, pager, next,jumper"
                  :current-page="queryParams.pageNum"
                  :page-size="queryParams.pageSize"
                  :page-sizes="[10,15,20]"
                  :total="total"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
          />
      </div>

    <!-- 添加或修改幻灯片子项表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="打开方式" prop="target">
          <el-select v-model="form.target" placeholder="请选择" clearable>
            <el-option :label="'新窗口(_blank)'" :value="'_blank'"/>
            <el-option :label="'当前窗口(_self)'" :value="'_self'"/>
            <el-option :label="'父窗口(_parent)'" :value="'_parent'"/>
            <el-option :label="'主窗口(_top)'" :value="'_top'"/>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="幻灯片内容" prop="content">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <single-upload v-model="form.image"style="width: 300px;display: inline-block;margin-left: 10px"></single-upload>
        </el-form-item>
        <el-form-item label="排序" prop="sortOrder">
          <el-input-number v-model="form.sortOrder" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/website/slideitem";
import SingleUpload from '@/components/Upload/singleUpload'

export default {
  name: "Item",
  components: { SingleUpload },
  dicts: ['sys_show_list'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 幻灯片子项表表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        target: '_blank'
      },
      // 图片预览
      dialogVisible: false,
      // 图片预览地址
      dialogImageUrl:null,
      // 表单校验
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur"}],
      }
    };
  },
  created() {
    const slideId = this.$route.params && this.$route.params.slideId;
    this.queryParams.slideId = slideId;
    this.getList();
  },
  methods: {
    /** 查询幻灯片子项表列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        slideItemId: null,
        slideId: null,
        title: null,
        image: null,
        url: null,
        target: null,
        description: null,
        content: null,
        isShow: null,
        sortOrder: null,
        more: null,
        updatedTime: null,
        updatedBy: null,
        createdTime: null,
        createdBy: null,
        deleted: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.slideItemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加幻灯片数据";
      this.form.slideId = this.queryParams.slideId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const slideItemId = row.slideItemId || this.ids
      getItem(slideItemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改幻灯片数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.slideItemId != null) {
            updateItem(this.form.slideItemId,this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const slideItemIds = row.slideItemId || this.ids;
      this.$modal.confirm('是否确认删除幻灯片子项表编号为"' + slideItemIds + '"的数据项？').then(function() {
        return delItem(slideItemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 预览图片
    handlePreview(url) {
      console.log(url)
      this.dialogVisible = true;
      this.dialogImageUrl = url;
    },
    handleSizeChange(val) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = val;
        this.getList();
    },
    handleCurrentChange(val) {
        this.queryParams.pageNum = val;
        this.getList();
    },
  }
};
</script>
